package com.wbl.controller;

import com.bigdata.domain.UserInfo;
import com.bigdata.service.DataInfoService;
import com.bigdata.tools.FtpUtil;
import com.mongodb.util.JSON;
import com.wbl.service.OuterDataQueryService;
import com.wbl.service.ProvService;
import jdk.jfr.events.FileWriteEvent;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.List;

/**
 * Created by hrd on 2016/8/8.
 */
@Controller
@RequestMapping("/studentQuery")
public class OuterDataController {
    private Logger logger= Logger.getLogger(OuterDataController.class);
    @Autowired
    private OuterDataQueryService outerDataQueryService;

    @Autowired
    private DataInfoService dataInfoService;
    @Autowired
    private ProvService provService;

    @RequestMapping("/queryCourse")  //url
    @ResponseBody
    public List<JSONObject> queryCourse(@RequestParam("stuId")String stuId,HttpServletRequest request) throws IOException{
        List<JSONObject> results=outerDataQueryService.QueryCouseById(stuId);
        String fileName="s"+stuId+"_course.json";
        FileOutputStream fos=new FileOutputStream("F:\\sharefile\\private\\"+fileName);
        fos.write(results.toString().getBytes("UTF-8"));
        fos.flush();
        fos.close();
        logger.debug("the new data in f:,which name is" + fileName);
        UserInfo currentUser=(UserInfo)request.getSession().getAttribute("userinfo");
        //文件上传
        File dataFile=new File("F:\\sharefile\\private\\"+fileName);
        InputStream ins=new FileInputStream(dataFile);
        String[]strs=dataFile.getName().split("\\.");
        String type=strs[1];
        try{
            FtpUtil.fileUploadToPrivate(ins,strs[0],dataFile.getName(),currentUser.getUserName());
        }catch (Exception e)
        {
            logger.debug("upload is error");
            e.printStackTrace();
        }
        queryCourseRecordData(fileName,type,"course",request);
        return results;

    }
    public void queryCourseRecordData(String fileName,String type,String originalDataName,HttpServletRequest request){
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("userinfo");
        provService.recordSplit(fileName,type,originalDataName,userInfo);
    }












}
